package com.git.hui.yinshua.biz.goods.service;

import com.git.hui.yinshua.biz.goods.dao.domain.GoodsAttributeKeyEntity;
import com.git.hui.yinshua.biz.goods.dao.domain.GoodsAttributeValueEntity;
import com.git.hui.yinshua.biz.goods.dao.repository.AttributeRepository;
import com.git.hui.yinshua.biz.goods.service.convert.GoodsConvert;
import com.git.hui.yinshua.biz.rest.models.goods.req.AttrKeyReq;
import com.git.hui.yinshua.biz.rest.models.goods.req.AttrValueReq;
import com.git.hui.yinshua.biz.rest.models.goods.rsp.AttrKeyDto;
import com.git.hui.yinshua.biz.rest.models.goods.rsp.AttrValDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品属性，尺寸规格之类的服务方法
 *
 * @author YiHui
 * @date 2024/6/13
 */
@Service
public class AttributeService {
    @Autowired
    private AttributeRepository attributeRepository;

    public AttrKeyDto addKey(AttrKeyReq key) {
        GoodsAttributeKeyEntity entity = attributeRepository.addAttributeKey(key);
        return GoodsConvert.toKey(entity);
    }

    public AttrKeyDto updateKey(AttrKeyReq key) {
        GoodsAttributeKeyEntity entity = attributeRepository.updateAttributeKey(key);
        return GoodsConvert.toKey(entity);
    }

    public boolean deleteKey(Integer keyId) {
        return attributeRepository.deleteAttributeKey(keyId);
    }

    public List<AttrKeyDto> listKeys(Integer cid) {
        List<GoodsAttributeKeyEntity> list;
        if (cid == null) {
            list = attributeRepository.listAllKeys();
        } else {
            list = attributeRepository.listKeysForCid(cid);
        }
        return GoodsConvert.batchToKey(list);
    }

    public AttrValDto addVal(AttrValueReq req) {
        GoodsAttributeValueEntity entity = attributeRepository.addAttributeValue(req.getKeyId(), req.getValue());
        return GoodsConvert.toVal(entity);
    }

    public boolean updateVal(AttrValueReq req) {
        return attributeRepository.updateAttributeValue(req.getValId(), req.getValue());
    }

    public boolean deleteVal(Integer valId) {
        return attributeRepository.deleteAttributeValue(valId);
    }

    public List<AttrValDto> listVals(Integer keyId) {
        List<GoodsAttributeValueEntity> list = attributeRepository.listValuesByKey(keyId);
        return GoodsConvert.batchToVal(list);
    }
}
